﻿// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}


int globlePid = 0;

//提升权限
extern "C" _declspec(dllexport) void EnableDebugPriv()
{
    HANDLE hToken;
    LUID sedebugnameValue;
    TOKEN_PRIVILEGES tkp;

    if (!OpenProcessToken(GetCurrentProcess(),
        TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
        return;
    }
    if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sedebugnameValue)) {
        CloseHandle(hToken);
        return;
    }
    tkp.PrivilegeCount = 1;
    tkp.Privileges[0].Luid = sedebugnameValue;
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(tkp), NULL, NULL)) {
        CloseHandle(hToken);
        return;
    }
}


//冻结
extern "C" _declspec(dllexport) void Freeze()
{
    //枚举进程信息
    PROCESSENTRY32 pe32;
    pe32.dwSize = sizeof(pe32);
    HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

    int processPid;
    //CString strTmp;
    BOOL b = ::Process32First(hProcessSnap, &pe32);
    while (b)
    {
        processPid = pe32.th32ProcessID;
        //char* exeFile = (char*)malloc()(pe32.szExeFile);
        if (strcmp(pe32.szExeFile, "winlogon.exe") == 0)
        {
            break;
        }

        //free(exeFile);

        b = ::Process32Next(hProcessSnap, &pe32);
    }
    ::CloseHandle(hProcessSnap);

    THREADENTRY32 th32;
    th32.dwSize = sizeof(th32);
    HANDLE hThreadSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
    globlePid = processPid;
    unsigned long Pid;
    Pid = processPid;
    b = ::Thread32First(hThreadSnap, &th32);
    while (b)
    {
        if (th32.th32OwnerProcessID == Pid)
        {
            HANDLE oth = OpenThread(THREAD_ALL_ACCESS, FALSE, th32.th32ThreadID);
            if (!(::SuspendThread(oth)))
            {
                //qDebug() << "freeze successed";
            }
            else
            {
                //qDebug() << "freeze failed";
            }
            CloseHandle(oth);
            break;
        }
        ::Thread32Next(hThreadSnap, &th32);
    }
    ::CloseHandle(hThreadSnap);
}

extern "C" _declspec(dllexport) void unFreeze()
{
    unsigned long Pid;
    Pid = globlePid;

    THREADENTRY32 th32;
    th32.dwSize = sizeof(th32);

    HANDLE hThreadSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
    BOOL b = ::Thread32First(hThreadSnap, &th32);
    while (b)
    {
        if (th32.th32OwnerProcessID == Pid)
        {
            HANDLE oth = OpenThread(THREAD_ALL_ACCESS, FALSE, th32.th32ThreadID);
            if (::ResumeThread(oth))
            {
                //qDebug() << "unfreeze successed";
            }
            else
            {
                //qDebug() << "unfreeze failed";
            }
            CloseHandle(oth);
            break;
        }
        ::Thread32Next(hThreadSnap, &th32);
    }
    ::CloseHandle(hThreadSnap);
}

//

